﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserFrm.aspx.cs" Inherits="WebApp.UserFrm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>用户信息</title>
    <link rel="stylesheet" type="text/css" href="css/newStyle.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/icon.css" />

    <script type="text/javascript" src="js/common.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery.easyui.min.js"></script>

    <script type="text/javascript">
        //页面加载
        $(window).load(function() {
            initData();
        });

        $(window).resize(function() {
            $('#tb').datagrid('resize');
        });

        function doSearch() {
            var params = getParams();
            $('#tb').datagrid('reload', params);
            //            if (data) {
            //                if (data.rows.length > 0) {
            //                    var qCode = $('#qCode').val();
            //                    var qUserName = $('#qUserName').val();
            //                    if (data.rows.code.indexOf(qCode) | data.userName.indexOf(qUserName))
            //                        return data;
            //                    else
            //                        return null;
            //                }
            //            }
        }

        function getParams() {
            return $.extend({}, {
                DataType: 'User',
                Code: $('#qCode').val(),
                UserName: $('#qUserName').val()
            });
        }


        //加载表格datagrid
        function initGrid() {
            var params = getParams();
            //加载数据
            $('#tb').datagrid({
                toolbar: '#searchBar',
                width: 'auto',
                fit: true,
                fitColumns: true,
                height: 'auto',
                striped: true,
                singleSelect: true,
                url: 'JSONHandler.ashx?DataType=User',
                queryParams: params,
                loadMsg: '数据加载中请稍后……',
                pagination: false,
                rownumbers: true,
                columns: [[
        { field: 'id', title: 'id', align: 'center', hidden: true },
        { field: 'code', title: '用户帐号', align: 'center' },
        { field: 'userName', title: '用户名称', align: 'center' },
        { field: 'role', title: '用户角色', align: 'center', formatter: roleFormat },
        { field: 'sex', title: '性别', align: 'center' },
        { field: 'post', title: '职务', align: 'center' },
        { field: 'mobile', title: '联系手机', align: 'center' },
        { field: 'tel', title: '联系电话', align: 'center' },
        { field: 'leader', title: '上级主管', align: 'center', formatter: headerFormat }
    ]], onDblClickRow: function(rowIndex, rowData) {
        editUser();
    }, onLoadSuccess: function(data) { }

            });

        }

        var mRoleData = null;
        var mUserData = null;
        function initData() {
            $.getJSON("JSONHandler.ashx?DataType=SysRoleType", function(result) {
                mRoleData = result;
                $.getJSON("JSONHandler.ashx?DataType=User", function(result) {
                    mUserData = result;
                    initGrid();
                });
            });


        }

        function roleFormat(val, rec) {
            if (mRoleData) {
                for (var i = 0; i < mRoleData.length; i++) {
                    if (mRoleData[i].Value == val)
                        return mRoleData[i].Name
                }
            }
            return '';
        }

        function headerFormat(val, rec) {
            if (mUserData) {
                for (var i = 0; i < mUserData.rows.length; i++) {
                    if (mUserData.rows[i].id == val)
                        return mUserData.rows[i].userName
                }
            }
            return '';
        }

        function resetAll() {
            $('#fm').form('clear');
        }

        function addUser() {
            openWin('EditUserFrm.aspx', "新增用户资料", 600, 400);
        }

        function editUser() {
            var row = $('#tb').datagrid('getSelected');
            if (row) {
                var userId = row.id;
                openWin('EditUserFrm.aspx?UserId=' + userId, "编辑用户资料", 600, 400);
            }
        }

        function deleteUser() {
            var row = $('#tb').datagrid('getSelected');
            if (row) {
                $.messager.confirm('提示', '确认删除该项吗?', function(r) {
                    if (r) {
                        WebApp.UserFrm.DeleteUser(row.id, function callback(p) {

                            if (p.error) {
                                $.messager.alert('异常消息', '提交删除异常,' + p.error.Message, 'error');
                                return;
                            }
                            var res = p.value;
                            if (res) {
                                var data = res.Data;
                                var msg = res.Msg;
                                if (msg)
                                    $.messager.alert('删除异常', msg, 'error');
                                else {
                                    if (data == true) {
                                        $.messager.alert('消息', '信息已删除!', 'info');
                                        doSearch();
                                    }
                                    else
                                        $.messager.alert('删除失败', msg, 'error');
                                }
                            }
                        });
                    }
                });
            }
        }

        function openWin(url, title, width, height) {
            $('#winFrame').prop('src', url);
            $('#winFrame').prop('style').height = height + 'px';
            $('#winFrame').prop('style').width = width + 'px';
            $('#win').window({
                title: title,
                width: width + 'px',
                height: height + 'px',
                modal: true
            });
        }      
    </script>

</head>
<body style="padding: 5px">
    <form id="form1" runat="server">
    </form>
    <div id="searchBar" style="padding: 5px; height: auto">
        <form id="fm" style="padding: 0; margin: 0">
        <span>帐号：</span><input type="text" style="width: 100px" id="qCode">
        <span>名称：</span><input type="text" style="width: 100px" id="qUserName">
        <a href="#" class="easyui-linkbutton" iconcls="icon-search" plain="true" onclick="doSearch();">
            搜索</a> <a href="#" class="easyui-linkbutton" iconcls="icon-undo" plain="true" onclick="resetAll();">
                重置</a> <a href="#" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="addUser();">
                    新增</a> <a href="#" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="deleteUser();">
                        删除</a></form>
    </div>
    <table id="tb" class="easyui-datagrid">
    </table>
    <div id="win">
        <iframe id="winFrame" src="" style="width: 0px; height: 0px" frameborder="0" scrolling="no">
        </iframe>
    </div>
</body>
</html>
